package com.shushan.accessibility;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.android.permission.OpenAutoStartUtil;
import com.shushan.base.Constants;
import com.shushan.permission.PermissionAccessibilityActivity;
import com.shushan.receiver.LockAdminReceiver;
import com.shushan.shushanbao.R;
import com.shushan.util.OpenSettingUtils;


/**
 * @ClassName: permissionOpenActivity
 * @Desciption: 用于启动设置页面
 * @author: yichaohua
 * @date: 2018-07-07
 */
public class PermissionOpenActivity extends Activity {

    private static final String TAG = "PermissionOpenActivity";

    private boolean isFirstCome = true;

    private int openIndex = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        if (intent != null && intent.getExtras() != null) {
            String action = intent.getStringExtra(Constants.ACTION);
            boolean result = intent.getBooleanExtra("result",false);
            int resultCode = intent.getIntExtra("resultCode",0);
            Log.i(TAG,"onNewIntent() action=" + action + " result=" + result + " resultCode=" + resultCode);

            if (Constants.ACTION_FINISH_SELF.equals(action)) {
                onBackAction(result,resultCode);
                return;
            }
        }

        openIndex = getIntent().getIntExtra("openIndex",0);
        Log.i(TAG,"openIndex=" + openIndex);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent != null && intent.getExtras() != null) {
            String action = intent.getStringExtra(Constants.ACTION);
            boolean result = intent.getBooleanExtra("result",false);
            int resultCode = intent.getIntExtra("resultCode",0);
            Log.i(TAG,"onNewIntent() action=" + action + " result=" + result + " resultCode=" + resultCode);

            if (Constants.ACTION_FINISH_SELF.equals(action)) {
                onBackAction(result,resultCode);
            }

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!isFirstCome) {
            finish();
        }else {
            openSetting(openIndex);
        }
        isFirstCome = false;
    }

    private void onBackAction(boolean result,int resultCode) {
        if(resultCode > 0){
            Intent intent1 = new Intent(this,PermissionAutoOpenActivity.class);
            intent1.putExtra("isAccessResult",true);
            intent1.putExtra("result",result);
            intent1.putExtra("resultCode",resultCode);
            startActivity(intent1);
        }

        finish();
    }

    private void openSetting(int openIndex) {
        switch (openIndex){
            case 1:
                OpenSettingUtils.openPowerSetting(this);
                OpenAccessiSettingHelper.closeSavePower(this);
                break;
            case 2:
                OpenAutoStartUtil.jumpStartInterface(this);
                OpenAccessiSettingHelper.openComplete(this);
                break;
            case 3:
                OpenSettingUtils.openFloatManager(this);
                OpenAccessiSettingHelper.openFloatManager(this);
                break;
            case 4:
                OpenSettingUtils.openUsageSetting(this);
                OpenAccessiSettingHelper.openUsageSetting(this);
                break;
            case 5:
                if(!checkAndEnableAdmin()){
                    OpenAccessiSettingHelper.enableAdmin(this);
                }else {
                    Intent intent = new Intent();
                    intent.putExtra("result", true);
                    intent.putExtra("resultCode", 400);
                    intent.putExtra(Constants.ACTION, Constants.ACTION_FINISH_SELF);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.setClass(this, PermissionOpenActivity.class);
                    startActivity(intent);
                }

                break;
            default:
                break;
        }
    }

    private boolean checkAndEnableAdmin() {
        boolean enableAdmin = false;
        DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
        ComponentName adminComponent = new ComponentName(this, LockAdminReceiver.class);
        if (!dpm.isAdminActive(adminComponent)) {
            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminComponent);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, this.getString(R.string.device_admin_hint));
            startActivityForResult(intent, 150);
            enableAdmin = false;
        } else {
            Log.i(TAG,"设备管理器已激活");
            enableAdmin = true;
        }

        return enableAdmin;
    }
}
